//
//  ZQEssenceViewController.m
//  BuDeJie
//
//  Created by 仲琦 on 16/4/10.
//  Copyright © 2016年 仲琦. All rights reserved.
//

#import "ZQEssenceViewController.h"
#import "ZQTitleButton.h"
#import "ZQConst.h"


#import "ZQAllViewController.h"
#import "ZQVideoViewController.h"
#import "ZQVoiceViewController.h"
#import "ZQPictureViewController.h"
#import "ZQWordViewController.h"
@interface ZQEssenceViewController () <UIScrollViewDelegate>

//标题栏
@property (nonatomic ,weak) UIView *titleView;

//上次选定的按钮
@property (nonatomic ,weak) ZQTitleButton *previousClickedTitleButton;

//下划线
@property (nonatomic ,weak)UIView *lineView;

//scrollView
@property (nonatomic ,weak)UIScrollView *scrollView;



@end

@implementation ZQEssenceViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    //添加导航控制器按钮
    [self Addnav];
    
    //添加所有子控制器
    [self setupAllChild];
    
    //添加ScrollView
    [self setupScrollView];
    
    //添加标题栏
    [self setupTitleView];
    
    // 默认显示第0个子控制器的view
    [self addChildVcViewIntoScrollView:0];
    
}

//添加所有子控制器
- (void)setupAllChild
{
    [self addChildViewController:[[ZQAllViewController alloc] init]];
    [self addChildViewController:[[ZQVideoViewController alloc] init]];
    [self addChildViewController:[[ZQVoiceViewController alloc] init]];
    [self addChildViewController:[[ZQPictureViewController alloc] init]];
    [self addChildViewController:[[ZQWordViewController alloc] init]];
    
    
}

//添加ScrollView
- (void)setupScrollView
{
    //创建ScrollView
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.backgroundColor = [UIColor greenColor];
    scrollView.frame = CGRectMake(0, 0, ZQScreenW, ZQScreenH);
    
    //设置取消显示滚动条
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    
    //点击状态栏回到顶部  , 取消原本的设置
    scrollView.scrollsToTop = NO;
    
    //设置滚动
    scrollView.contentSize = CGSizeMake(ZQScreenW * 5, 0);
    scrollView.pagingEnabled = YES;
    
    [self.view addSubview:scrollView];
    self.scrollView  = scrollView;
    scrollView.delegate = self;
    

    
//    NSInteger count = self.childViewControllers.count;
//    
//    for (int a = 0 ; a <count ; a++) {
//        
//        UIView *childView = self.childViewControllers[a].view;
//        childView.frame = CGRectMake(a * scrollView.zq_width, 0, scrollView.zq_width, scrollView.zq_height);
//        
//        [scrollView addSubview:childView];
//    }
    
    //设置不要自动调整内边距
    self.automaticallyAdjustsScrollViewInsets = NO;
}



//添加标题栏
- (void)setupTitleView
{
    //创建标题栏
    UIView *titleView = [[UIView alloc]init];
    titleView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
    titleView.frame = CGRectMake(0, ZQNavBarMaxY, ZQScreenW, ZQTitleViewH);
    [self.view addSubview:titleView];
    self.titleView = titleView;
    
    //添加所有按钮
    [self addAllTitleButton];
    //添加下划线
    [self addTitleLine];
    

    
}

//添加下划线
- (void)addTitleLine
{
    //取出标题栏的第一个子控件
    ZQTitleButton *firstBtn = self.titleView.subviews.firstObject;
    
    //创建下划线
    UIView *lineView = [[UIView alloc]init];
    lineView.backgroundColor = [UIColor redColor];
    CGFloat lineH = 2;
    CGFloat lineY = self.titleView.zq_height - lineH;
    lineView.frame = CGRectMake(0, lineY, 0, lineH);
    [self.titleView addSubview:lineView];
    self.lineView = lineView;
    
    //设置选定按钮赋值
    firstBtn.selected = YES;
    self.previousClickedTitleButton = firstBtn;
    
    //手动计算第一个按钮的文字尺寸
    [firstBtn.titleLabel sizeToFit];
    
    //设置下划线尺寸
    lineView.zq_width = firstBtn.titleLabel.zq_width + 10;
    lineView.zq_centerX = firstBtn.zq_centerX;
    
}

//添加所有按钮
- (void)addAllTitleButton
{
    
    NSArray *nameArray = @[@"全部", @"视频", @"声音", @"图片", @"段子"];
    
    NSInteger count = nameArray.count;
    
    CGFloat btnW = self.titleView.zq_width / count;
    CGFloat btnH = self.titleView.zq_height;
    for (int a = 0; a < count ; a++) {
        ZQTitleButton *titleBtn = [[ZQTitleButton alloc]init];
        [titleBtn setTitle:nameArray[a] forState:UIControlStateNormal];
        [titleBtn addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        titleBtn.tag = a;
        titleBtn.frame = CGRectMake(a * btnW, 0, btnW, btnH);
        
        [self.titleView addSubview:titleBtn];
        
    }
    
    
}

//标题栏按钮点击方法
- (void)titleButtonClick:(ZQTitleButton *)btn
{
    
    //判断 上次点击标题与这次点击的标题相同,  就发出通知
    if (self.previousClickedTitleButton == btn) {
        [[NSNotificationCenter defaultCenter] postNotificationName:ZQTitleButtonDidRepeatClickNotification object:nil];
    }
    
    
    self.previousClickedTitleButton.selected = NO;
    
    btn.selected = YES;
    
    self.previousClickedTitleButton = btn;
    
    
    NSInteger index = btn.tag;

    //动画效果移动下划线
    [UIView animateWithDuration:0.25 animations:^{
        self.lineView.zq_width = btn.titleLabel.zq_width + 10;
        self.lineView.zq_centerX = btn.zq_centerX;

        self.scrollView.contentOffset = CGPointMake(index *ZQScreenW, 0);
    } completion:^(BOOL finished) {
        [self addChildVcViewIntoScrollView:index];
    }];
    

    
    //点击状态栏 回到顶部
    //遍历子控件
    for (int a = 0; a < self.childViewControllers.count; a++) {
        
        //取出子控件
        UIViewController *childVC = self.childViewControllers[a];
        
        //如果还没有被控制器创建  跳过
        if (!childVC.isViewLoaded) continue;
        
        //如果view不是scrollview的类 跳过
        if (![childVC.view isKindOfClass:[UIScrollView class]]) continue;
        
        //取出ScrollView
        UIScrollView *scrollV = (UIScrollView *)childVC.view;
        
        //被点击的按钮的对应控制器 scrollsToTop 为yes
        if (a == index) {
            scrollV.scrollsToTop = YES;
        }else{
            scrollV.scrollsToTop = NO;
        }
        
    }
    
}


//添加index位置的子控制器view到scrollView中
- (void)addChildVcViewIntoScrollView:(NSInteger)index
{
    UIView *childView = self.childViewControllers[index].view;
    childView.frame = CGRectMake(index * self.scrollView.zq_width, 0, self.scrollView.zq_width, self.scrollView.zq_height);
    [self.scrollView addSubview:childView];
    
}


//添加导航控制器按钮
- (void)Addnav
{
    
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"nav_item_game_icon"] highImage:[UIImage imageNamed:@"nav_item_game_click_icon"] target:self action:@selector(game)];
    
    self.navigationItem.leftBarButtonItem = leftItem;
    
    // 右边
    UIBarButtonItem *rightItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"navigationButtonRandom"] highImage:[UIImage imageNamed:@"navigationButtonRandomClick"] target:nil action:nil];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    // 中间 titleView
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
}

- (void)game
{
    NSLog(@"游戏");
}



//代理监听 scrollView的滚动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{

    NSInteger count = scrollView.contentOffset.x / ZQScreenW ;
    
    ZQTitleButton *btn = self.titleView.subviews[count];
    
    // 如果上一次点击的按钮 和 这次想要点击的按钮 相同，直接返回
    if (self.previousClickedTitleButton == btn) return;
    
    [self titleButtonClick:btn];
}

@end
